package com.example.guessthesoundtrack;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.Paint.Align;
import android.view.MotionEvent;

public class Shadow extends Sprite {
	private Letter letter;
	
	private Bitmap image;
	private Paint paint;
	private Rect bounds;
	
	private boolean space, highlighted;

	public Shadow(Bitmap unclicked, Bitmap image, float x, float y, float width, float height, boolean space) {
		super(unclicked, unclicked, new Rect((int)x, (int)y, (int)(x + width), (int)(y + height)), x, y, width, height);
		this.image = image;
		this.space = space;
		this.highlighted = false;
		this.letter = null;
		
		bounds = new Rect();
		paint = new Paint();
		paint.setTextSize(32f);
		paint.setTextAlign(Align.CENTER);
		paint.setTypeface(Typeface.DEFAULT_BOLD);
	}

	public Letter getLetter() {
		return letter;
	}

	public void setLetter(Letter letter) {
		this.letter = letter;
		letter.setShadow(this);
	}
	
	public void removeLetter() {
		letter.setShadow(null);
		letter = null;
	}

	public Bitmap getImage() {
		return image;
	}

	public void setImage(Bitmap image) {
		this.image = image;
	}

	public Paint getPaint() {
		return paint;
	}

	public void setPaint(Paint paint) {
		this.paint = paint;
	}

	public Rect getBounds() {
		return bounds;
	}

	public void setBounds(Rect bounds) {
		this.bounds = bounds;
	}

	public boolean isSpace() {
		return space;
	}

	public void setSpace(boolean space) {
		this.space = space;
	}

	public boolean isHighlighted() {
		return highlighted;
	}

	public void setHighlighted(boolean highlighted) {
		this.highlighted = highlighted;
	}

	@Override
	public void draw(Canvas canvas) {
		if (space)
			return;
		
		if (letter == null) {
			canvas.drawBitmap((highlighted ? image : getUnclicked()), null, getRect(), null);
		} else {
			canvas.drawBitmap((isDown() ? letter.getClicked() : letter.getUnclicked()), null, getRect(), null);
			canvas.drawText("" + letter.getLetter(), getX() + (getWidth() / 2f), getY() + (getHeight() / 2f) + (bounds.height() / 2f) + ((getHeight() / 16f) * (isDown() ? 1 : -1)), paint);
		}
	}

	@Override
	public void update(int delta) { }

	@Override
	public void onClick(MotionEvent event, float x, float y) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			setDown(true);
			break;
		case MotionEvent.ACTION_MOVE:
			setDown(isColliding(x, y));
			break;
		case MotionEvent.ACTION_UP:
			setDown(false);
			if (letter != null && isColliding(x, y))
				removeLetter();
			break;
		}
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean isColliding(float x, float y) {
		if (!space)
			return (x >= getX() && y >= getY() & x <= getX() + getWidth() && y <= getY() + getHeight());
		return false;
	}
}